package app.kotlin_1.adapter

import android.content.Context
import android.content.Intent
import android.graphics.Bitmap
import android.graphics.drawable.BitmapDrawable
import android.os.Bundle
import android.support.v4.view.PagerAdapter
import android.support.v7.widget.RecyclerView
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.ImageView
import android.widget.TextView
import app.kotlin_1.R
import app.kotlin_1.activity.GameDetailActivity
import app.kotlin_1.bean.GameBean
import app.kotlin_1.utils.ImageUtil
import app.kotlin_1.utils.toast
import com.facebook.drawee.backends.pipeline.Fresco
import com.facebook.drawee.view.SimpleDraweeView

/**
 * Created by 刘智强 on 2017/9/21.
 *游戏分类的viewpager的适配器
 */
class GameKindViewPagerAdapter : PagerAdapter {
    private var context: Context? = null
    private var viewList = mutableListOf<View>()
    private var gameList = mutableListOf<GameBean>()

    constructor(context: Context?, gameList: MutableList<GameBean>) : super() {
        this.context = context
        this.gameList = gameList
        gameList.forEach {
            var rootView = LayoutInflater.from(context).inflate(R.layout.item_game_kind_viewpager, null, false)
            var name = rootView.findViewById(R.id.tvGameName) as TextView
            var image = rootView.findViewById(R.id.sdvGameIcon) as SimpleDraweeView
            image.scaleType = ImageView.ScaleType.CENTER_CROP
            var gameName = it.name
            name.text = gameName
            image.setImageURI(it.icon)

            viewList.add(rootView)
        }
    }


    override fun isViewFromObject(view: View?, `object`: Any?): Boolean {
        return view == `object`
    }

    override fun getCount(): Int {
        return viewList.size
    }

    override fun instantiateItem(container: ViewGroup?, position: Int): Any {

        container?.addView(viewList[position])
        viewList[position].setOnClickListener{
            val intent = Intent(context, GameDetailActivity::class.java)
            val bundle = Bundle()
            bundle!!.putSerializable("game",gameList[position] )
            intent.putExtras(bundle)
            context!!.startActivity(intent)
        }
//        viewList[position].setOnClickListener {
//            context!!.toast(gameList[position].name)
//        }
        return viewList[position]
    }

    override fun destroyItem(container: ViewGroup?, position: Int, `object`: Any?) {
        container!!.removeView(viewList[position])
    }

}
